<?php

defined('SYSPATH') or die('Access error!');

/**
 * 问答平台的ajax类
 * ============================================================================
 * 功能描述： 
 *      用户的初始化以及常用的一些方法
 * ============================================================================
 * $Author: liuxudong $
 */
class Controller_Q2a_Ajax extends Controller
{

    protected $settings = array();

    public function before()
    {
        parent::before();
        I18n::set_current_file('question');
    }

    public function __construct(Request $request)
    {
        $this->settings = Model_Setting::factory('setting')->get_all_setting();
        parent::__construct($request);
        header('Content-Type', 'application/json');
        if (!Request::$is_ajax)
        {
            $this->request->redirect(URL::site());
            return;
        }
    }

    //获取某一个问题的投票
    public function action_get_votes()
    {
        $id = Arr::get($_POST, 'id', '');
        $act = Arr::get($_POST, 'act', '');
        $votes = array();
        $message = '';
        if (!empty($id))
        {
            $message = '';
            $post = Model_Q2a_Post::factory('q2a_post');
            $post_detail = $post->get_row(array('id' => $id), 'user_id,up_votes,down_votes');

            if ($act != '')
            {
                $current_user = Session::instance()->get('user');
                if (!count($current_user) > 0 && empty($current_user))
                {
                    $message = '请先登录';
                }
                elseif ($post_detail['user_id'] == $current_user['id'])
                {
                    $message = '您不能给自己投票!';
                }
                elseif (Cookie::get('votes_' . $id))
                {
                    $message = '您已经投票！';
                }
                else
                {
                    if ($act == 'up')
                    {
                        $data['up_votes'] = $post_detail['up_votes'] + 1;
                    }
                    elseif ($act == 'down')
                    {
                        $data['down_votes'] = $post_detail['down_votes'] + 1;
                    }

                    $post->update($data, array('id' => $id));
                    Cookie::set('votes_' . $id, 'votes' . $id, Date::get_time() + 3600);
                }
            }
            $votes = $post->get_row(array('id' => $id), 'up_votes,down_votes');
            $votes['message'] = $message;
            $votes['id'] = $id;
            echo json_encode($votes);
        }
        exit;
    }

    //ajax添加提问的答案
    public function action_add_answer()
    {
        //验证码
        $captcha = Arr::get($_POST, 'catpcha', '');
        if (!Captcha::valid($captcha))
        {
            echo 0;
            exit;
        }
        $data = array();
        $data['content'] = Arr::get($_POST, 'content', '');
        $data['category_id'] = Arr::get($_POST, 'category_id', 0);
        $data['parent_id'] = Arr::get($_POST, 'parent_id', 0);
        $data['user_id'] = Session::instance()->get_detail('user', 'id');
        $data['notify_email'] = Session::instance()->get_detail('user', 'email');
        $data['latest_activity'] = Date::get_time();
        $data['add_time'] = Date::get_time();
        $data['update_time'] = Date::get_time();
        $data['post_type'] = Q2A_POST_TYPE_ANSWER;
        $answer = Model_Q2a_Answer::factory('q2a_answer');
        $html = '';
        if ($answer->insert_answer($data))
        {
            $question = Model_Q2a_Question::factory('q2a_question');
            $answer_detail = $question->get_row(array('id' => $data['parent_id']), 'answer_count,user_id');
            //插入问题的回复记录
            $question_arr = array();
            $question_arr['answer_count'] = $answer_detail['answer_count'] + 1;
            $question->update($question_arr, array('id' => $data['parent_id']));

            if (intval($this->settings['user_q2a_answer_points']) != 0)
            {
                Model_User_Points::factory('user_points')->save_points(Session::instance()->get_detail('user', 'id'), 'user_q2a_answer_points', $data['parent_id']);
            }
            $html .='<div class="other_answer_list_other">';
            $html .='<div class="answer_people">';
            $html .=' <img src="' . Session::instance()->get_detail('user', 'avatar_path') . '"/></div>';
            $html .='<div class="answer_people_right">';
            $html .=' <p>'.$data['content'].'</p>';
            $html .=' <p>'.Session::instance()->get_detail('user', 'username').'<span>回答于&nbsp;&nbsp;&nbsp;'.  Date::floortime($data['add_time']).'</span></p></div>';
            $html .=' <div class="clear"></div></div>';
        }
        unset($data);
        unset($question_arr);
        echo $html;
        exit;
    }

    //置顶
    public function action_answer_top()
    {
        $answer = Model_Q2a_Answer::factory('q2a_answer');
        $id = Arr::get($_POST, 'id', 0);

        //找到本问题
        $answer_detail = $answer->get_row(array('id' => $id), 'parent_id,user_id');

        if ($answer->is_exist(array('parent_id' => $answer_detail['parent_id'], 'is_top' => 1)))
        {
            echo '该问题已有最佳答案';
            exit;
        }

        if ($id != 0)
        {
            $data = array();
            $data['is_top'] = 1;
            if ($answer->update($data, array('id' => $id)))
            {


                //找到问题的积分
                $question_detail = $answer->get_row(array('id' => $answer_detail['parent_id']), 'points,user_id');
                if ($question_detail['points'] != 0)
                {
                    Model_User_Points::factory('user_points')->save_points($answer_detail['user_id'], array('q2a_reward_reply' => $question_detail['points']), $answer_detail['parent_id'], $question_detail['user_id']);
                }

                //置顶赠送积分
                if (intval($this->settings['q2a_answer_top_points']) != 0)
                {
                    Model_User_Points::factory('user_points')->save_points($answer_detail['user_id'], 'q2a_answer_top_points', $answer_detail['parent_id']);
                }
                echo 1;
                exit;
            }
        }
    }

    //管理员删除某一个答案
    public function action_delete_answer()
    {

        $id = Arr::get($_POST, 'id', 0);
        $answer = Model_Q2a_Answer::factory('q2a_answer');
        if ($answer->delete(array('id' => $id)))
        {
            echo 1;
            exit;
        }
        echo 0;
        exit;
    }

}